import Http, {PARAMS} from '@/api/http';
import ListObject from '@/structure/listObject';
import LoginLog from '@/structure/loginLog';
import LoginResponse from '@/structure/loginResponse';
import Menu from '@/structure/menu';
import PasswordInfo from '@/structure/passwordInfo';
import Position from '@/structure/position';
import TwoFactorAuthenticationBindInfo from '@/structure/twoFactorAuthenticationBindInfo';
import User from '@/structure/user';
import UserUtils from '@/utils/user';

const BASE = 'user/user/controlPanel';

/**
 * 控制面板
 */
class ControlPanelApi extends Http {
  /**
   * 用户名登录
   * @param data 登录请求
   * @return 登录响应
   */
  public login(data: any): Promise<LoginResponse<User>> {
    return super.post0(`${BASE}/login`, data);
  }

  /**
   * 手机号登录
   * @param data 登录请求
   * @return 登录响应
   */
  public loginByPhone(data: any): Promise<LoginResponse<User>> {
    return super.post0(`${BASE}/loginByPhone`, data);
  }

  /**
   * 邮箱地址登录
   * @param data 登录请求
   * @return 登录响应
   */
  public loginByEmail(data: any): Promise<LoginResponse<User>> {
    return super.post0(`${BASE}/loginByEmail`, data);
  }

  /**
   * 获取当前用户信息
   * @return 当前用户信息
   */
  // noinspection JSUnusedGlobalSymbols
  public getInfo(): Promise<User> {
    return super.get0(BASE);
  }

  /**
   * 刷新数据令牌
   * @return 登录响应
   */
  // noinspection JSUnusedGlobalSymbols
  public refreshToken(): Promise<LoginResponse<User>> {
    return super.get0(`${BASE}/refreshToken`, {
      headers: {
        refreshToken: UserUtils.getRefreshToken()
      }
    });
  }

  /**
   * 修改密码
   * @param data 修改密码请求
   * @return 响应
   */
  public updatePassword(data: PasswordInfo): Promise<void> {
    return super.post0(`${BASE}/password`, data);
  }

  /**
   * 更新个人信息
   * @param data 用户信息
   * @return 响应
   */
  // noinspection JSUnusedGlobalSymbols
  public updateProfile(data: User): Promise<void> {
    return super.post0(`${BASE}/profile`, data);
  }

  /**
   * 更新头像
   * @param data 头像
   * @return 响应
   */
  // noinspection JSUnusedGlobalSymbols
  public updateAvatar(data: File): Promise<User> {
    return super.post0(`${BASE}/avatar`, data);
  }

  /**
   * 删除头像
   * @return 响应
   */
  public deleteAvatar(): Promise<User> {
    return super.post0(`${BASE}/deleteAvatar`);
  }

  /**
   * 获取菜单列表
   * @return 菜单列表
   */
  public getMenus(): Promise<Menu[]> {
    const domains = process.env.VUE_APP_MENU_DOMAINS;
    if (domains && domains.length > 0) {
      return super.get0(`${BASE}/menus`, {
        params: {
          domains
        }
      });
    } else {
      return super.get0(`${BASE}/menus`);
    }
  }

  /**
   * 获取职务信息
   * @return 职务列表
   */
  public getPositions(): Promise<Position[]> {
    return super.get0(`${BASE}/positions`);
  }

  /**
   * 获取权限key列表
   * @return 权限key列表
   */
  public getPermissionKeys(): Promise<string[]> {
    return super.get0(`${BASE}/permissionKeys`);
  }

  /**
   * 获取双因素认证绑定信息
   * @return 双因素认证绑定信息
   */
  public getTwoFactorAuthenticationBindInfo(): Promise<TwoFactorAuthenticationBindInfo> {
    return super.get0(`${BASE}/twoFactorAuthenticationBindInfo`);
  }

  /**
   * 启用双因素认证绑定信息
   * @param secretKey 安全码
   * @param otp 动态密码
   * @return 响应
   */
  public enableTwoFactorAuthentication(secretKey: string, otp: string): Promise<LoginResponse<User>> {
    return super.post0(`${BASE}/enableTwoFactorAuthentication/${secretKey}`, {
      otp
    });
  }

  /**
   * 解绑双因素认证绑定信息
   * @param otp 动态密码
   * @return 响应
   */
  public disableTwoFactorAuthentication(otp: string): Promise<LoginResponse<User>> {
    return super.post0(`${BASE}/disableTwoFactorAuthentication`, {
      otp
    });
  }

  /**
   * 获取登录记录
   * @param params 搜索参数
   * @return 登录记录列表
   */
  public loginLogs(params: PARAMS = {}): Promise<ListObject<LoginLog>> {
    return super.get0(`${BASE}/loginLogs`, {
      params
    });
  }
}

export default new ControlPanelApi();
